package wfmanage.action;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.wfmanage.models.Lookup;

public class AdminTagUser extends ActionSupport {
	private String role;
	private String user;
	private int limit;
	private String commandButton = "";
	int role_id=1;
	private String role_name="";
	int user_id=1;
	private String user_name="";
	private String msg="";
	private String email_id="";
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String execute() throws Exception {
		 MyLog.log("Inside AdminTagUser : Method : execute");

	        HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	             if(param.equals("myRole")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup role=Lookup.findOne("role", "where role_name = '"+paramVal+"'");
	 	            	role_id=role.getId();
	 	            	role_name=role.getName();
	 	            	MyLog.log(paramVal);
	 	            }
	             }
	             else if(param.equals("myUser")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
		 	            for(String paramVal:paramValues)
		 	            {
		 	            	Lookup user=Lookup.findOne("user", "where user_name = '"+paramVal+"'");
		 	            	user_id=user.getId();
		 	            	user_name=user.getName();
		 	            	email_id=user.getEmail_id();
		 	                MyLog.log(paramVal);
		 	            }
	 	            	
	             }
	             
	        }
	
	        if (this.commandButton.startsWith("Tag")) {
	        	 int result=insert();
	        	 if(role_name.equals("user")){
	        		 limit=5000;
	        	 }
	        	 else if(role_name.equals("vefication_dpt")){
	        		 limit=8000;
	        	 }
	        	 else if(role_name.equals("finance_dpt")){
	        		 limit=10000;
	        	 }
	        	 
	        	 int res= updateRole(role_name,limit);
	 			if(result>0 && res>0){
	 				email mail=new email();
	 				mail.sendmail(user_name, email_id, role_name, 0,"NA");
           			msg="Tagged user "+user_name +" successfully to role "+role_name+"!! An E-mail Notification is sent to user e-mail id  "+email_id;
           			MyLog.log("tagged user successfully to role");
           		 }
           		 else{
           			msg="Cannot Tag, User "+user_name +" already tagged to the role "+role_name;
           			MyLog.log("cannot tag, user already tagged to this role");
           			return "displayWF";
           		 }
           	 	
           	 return "displayWF";
            }
            else if (this.commandButton.startsWith("Remove")) {
           	 int result=Lookup.findOnefordelete("User_Role"," where user_id="+user_id+" and role_id="+role_id);
           	 int res=updateRole("",0);
           	 if(result>0){
           		 msg="Removed user "+user_name +" successfully tagged to the role"+role_name+"!!";
       			 MyLog.log("tagged user successfully to role");
       		 }
       		 else{
       			 msg="Cannot Remove, User "+user_name +" tagged to the role "+role_name+". Please try again!!";
       			 MyLog.log("cannot tag, user tagged to this role");
       			 return "displayWF";
       		 }
           	return "displayWF";
            }
            else if (this.commandButton.startsWith("Back to Admin")) {
      			return "admin_home";
      		}
	
	    	return "error";
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	public int insert() {
		String insertSQL = "insert into User_Role(user_id,role_id) values" +
				"				("+user_id +", "+role_id+");";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
	public int updateRole(String role_name,int limit) {
		String insertSQL = "update employee set emp_role='"+role_name+"',LTA_LIMIT="+limit+" where user_name='"+user_name+"';";
		MyLog.log("update query"+insertSQL);
		return DB.update(insertSQL);
	}
}
